home *** CD-ROM | disk | FTP | other *** search
- #include "Offscreen.h"
-
- #define __EqualRect(r1,r2) \
- ( (*(long *)&(r1).top == *(long *)&(r2).top) && \
- (*(long *)&(r1).bottom == *(long *)&(r2).bottom) )
-
- // ---------------------------------------------------------------------------
- // • StOffscreenGWorld
- // ---------------------------------------------------------------------------
- // Constructor
- //
- // Upon entry, the current port must to set to where you want to copy
- // the offscreen image when the StOffscreenGWorld object is destroyed
- //
- // The inPixelDepth, inFlags, inCTableH, and inGDeviceH parameters
- // all have default values (see the header file), so you don't need
- // to specify them.
- //
- // However, in some cases, you may want to specify an absolute
- // pixel depth. It may also be useful to set the useTempMem flag
- // so that the offscreen pixels are allocated in temporary memory.
- // This is appropriate because StOffscreenGWorlds are meant to be
- // temporary, stack-based, objects.
-
- WindowOffscreen *DrawOffscreen( const Rect *inBounds, WindowPtr theWindow)
- {
- #if powerc
- short inPixelDepth = 0;
- GWorldFlags inFlags = useTempMem;
- CTabHandle inCTableH = nil;
- GDHandle inGDeviceH = nil;
- WindowOffscreen *theOffscreen;
- Rect bRect = (inBounds ? *inBounds : theWindow->portRect);
- Rect gwRect = bRect;
-
- QDErr err;
-
-
- SetPort(theWindow);
-
- if ((theOffscreen = (WindowOffscreen *) NewPtr(sizeof(WindowOffscreen))) == 0L)
- return(0L);
- // We need to save the current GWorld because the image from
- // the offscreen GWorld will be copied into this GWorld
- // in the Destructor.
-
- GetGWorld(&theOffscreen->mSavePort, &theOffscreen->mSaveDevice);
-
- theOffscreen->mBounds = bRect;
-
- theOffscreen->mMacGWorld = nil;
-
- // NewGWorld interprets the bounds in global coordinates
- // when specifying a zero pixel depth. It uses the maximum
- // depth of all screen devices intersected by the bounds.
-
- if (inPixelDepth == 0) {
- LocalToGlobal(&topLeft(gwRect));
- LocalToGlobal(&botRight(gwRect));
- }
-
- err = NewGWorld(&theOffscreen->mMacGWorld, inPixelDepth, &gwRect,
- inCTableH, inGDeviceH, inFlags);
-
- if(err || theOffscreen->mMacGWorld == nil) {
- DisposePtr((Ptr)theOffscreen);
- return(0L);
- }
-
- // Make offscreen GWorld the current one and prepare it
- // for drawing by setting the coordinate system, and locking
- // and erasing the pixels.
-
- SetGWorld(theOffscreen->mMacGWorld, nil);
- // SetOrigin(theOffscreen->mBounds.left, theOffscreen->mBounds.top);
- LockPixels(GetGWorldPixMap(theOffscreen->mMacGWorld));
- SetOrigin(theOffscreen->mBounds.left, theOffscreen->mBounds.top);
- ForeColor (blackColor);
- BackColor (whiteColor);
- EraseRect(&theOffscreen->mBounds);
- return theOffscreen;
- #else
- return nil;
- #endif
- }
-
- WindowOffscreen *DrawOffscreenSaved(WindowOffscreen *inOffscreen, const Rect *inBounds, WindowPtr theWindow)
- {
- #if powerc
- short inPixelDepth = 0;
- GWorldFlags inFlags = useTempMem;
- CTabHandle inCTableH = nil;
- GDHandle inGDeviceH = nil;
- WindowOffscreen *theOffscreen;
- Rect bRect = (inBounds ? *inBounds : theWindow->portRect);
- Rect gwRect = bRect;
-
- QDErr err= noErr;
-
- if(inOffscreen) {
- if(inBounds) {
- if(!__EqualRect(inOffscreen->mMacGWorld->portRect, *inBounds)) {
- DisposeGWorld(inOffscreen->mMacGWorld);
- DisposePtr((Ptr)inOffscreen);
- inOffscreen = nil;
- }
- }
- else {
- if(!__EqualRect(inOffscreen->mMacGWorld->portRect, theWindow->portRect)) {
- DisposeGWorld(inOffscreen->mMacGWorld);
- DisposePtr((Ptr)inOffscreen);
- inOffscreen = nil;
- }
- }
- }
-
-
-
- SetPort(theWindow);
-
- if(!inOffscreen) {
- if ((theOffscreen = (WindowOffscreen *) NewPtr(sizeof(WindowOffscreen))) == 0L)
- return(0L);
- }
- else {
- theOffscreen = inOffscreen;
- }
- // We need to save the current GWorld because the image from
- // the offscreen GWorld will be copied into this GWorld
- // in the Destructor.
-
- GetGWorld(&theOffscreen->mSavePort, &theOffscreen->mSaveDevice);
-
- theOffscreen->mBounds = bRect;
- if(!inOffscreen || inOffscreen->mMacGWorld == nil) {
- theOffscreen->mMacGWorld = nil;
-
- // NewGWorld interprets the bounds in global coordinates
- // when specifying a zero pixel depth. It uses the maximum
- // depth of all screen devices intersected by the bounds.
-
- if (inPixelDepth == 0) {
- LocalToGlobal(&topLeft(gwRect));
- LocalToGlobal(&botRight(gwRect));
- }
-
- err = NewGWorld(&theOffscreen->mMacGWorld, inPixelDepth, &gwRect,
- inCTableH, inGDeviceH, inFlags);
- }
- if(err || theOffscreen->mMacGWorld == nil) {
- DisposePtr((Ptr)theOffscreen);
- return(0L);
- }
-
- // Make offscreen GWorld the current one and prepare it
- // for drawing by setting the coordinate system, and locking
- // and erasing the pixels.
-
- SetGWorld(theOffscreen->mMacGWorld, nil);
- // SetOrigin(theOffscreen->mBounds.left, theOffscreen->mBounds.top);
- LockPixels(GetGWorldPixMap(theOffscreen->mMacGWorld));
- SetOrigin(theOffscreen->mBounds.left, theOffscreen->mBounds.top);
- ForeColor (blackColor);
- BackColor (whiteColor);
- EraseRect(&theOffscreen->mBounds);
- return theOffscreen;
- #else
- return nil;
- #endif
- }
-
- WindowOffscreen *DrawOffscreenNoInitialize(const Rect *inBounds, WindowPtr theWindow)
- {
- return DrawOffscreen(inBounds, theWindow);
- }
-
- // ---------------------------------------------------------------------------
- // • ~StOffscreenGWorld
- // ---------------------------------------------------------------------------
- // Destructor
- //
- // Restores the current Port to what it was before the StOffscreenGWorld
- // was created and copies the offscreen image to that Port
-
- void DrawOnscreen(WindowOffscreen *theOffscreen)
- {
- #if powerc
- // Restore current GWorld to the one which was current
- // when the Constructor was called
- if(theOffscreen) {
- SetGWorld(theOffscreen->mSavePort, theOffscreen->mSaveDevice);
-
- // Copy image from the offscreen GWorld to the current GWorld,
- // then destroy the offscreen GWorld
-
- if (theOffscreen->mMacGWorld != nil) {
-
- ForeColor (blackColor);
- BackColor (whiteColor);
-
- CopyBits(&((GrafPtr)theOffscreen->mMacGWorld)->portBits,
- &((GrafPtr)theOffscreen->mSavePort)->portBits,
- &theOffscreen->mBounds, &theOffscreen->mBounds, srcCopy, nil);
- UnlockPixels(GetGWorldPixMap(theOffscreen->mMacGWorld));
- DisposeGWorld(theOffscreen->mMacGWorld);
- }
- DisposePtr((Ptr)theOffscreen);
- }
- #endif
-
- }
-
- void DrawOnscreenSave(WindowOffscreen *theOffscreen)
- {
- #if powerc
- // Restore current GWorld to the one which was current
- // when the Constructor was called
- if(theOffscreen) {
- SetGWorld(theOffscreen->mSavePort, theOffscreen->mSaveDevice);
-
- // Copy image from the offscreen GWorld to the current GWorld,
- // then destroy the offscreen GWorld
-
- if (theOffscreen->mMacGWorld != nil) {
-
- ForeColor (blackColor);
- BackColor (whiteColor);
-
- CopyBits(&((GrafPtr)theOffscreen->mMacGWorld)->portBits,
- &((GrafPtr)theOffscreen->mSavePort)->portBits,
- &theOffscreen->mBounds, &theOffscreen->mBounds, srcCopy, nil);
- UnlockPixels(GetGWorldPixMap(theOffscreen->mMacGWorld));
- // DisposeGWorld(theOffscreen->mMacGWorld);
- }
- // DisposePtr((Ptr)theOffscreen);
- }
- #endif
- }
-
- void DisposeOffscreen(WindowOffscreen *theOffscreen)
- {
- if(theOffscreen) {
- if(theOffscreen->mMacGWorld) {
- DisposeGWorld(theOffscreen->mMacGWorld);
- }
- DisposePtr((Ptr)theOffscreen);
- }
- }